# -*- coding: utf-8 -*-
"""
Created on Fri Aug 17 13:51:37 2018

@author: Who

新财经
https://www.xcj.com/

"""

import requests
import json
import time
from cs import day
from WriteData import writedata
from Tk import genearteMD5


def get_html_text(url, data):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.post(url, timeout=30, data=data, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


data = {
    'page': '2',
    'newsclass': 'flash',
    'length': '15',
    'nowdate': '20180816',
    'keyword': ''
}
url = 'https://www.xcj.com/newsflash/index'


def get_dates_Kx(url, data):
    text = get_html_text(url, data)
    if not text:
        return None
    text = json.loads(text)
    dates = [time.strftime("%Y-%m-%d %X", time.localtime(date['createtime'])) for date in text['rows']]
    nextPage = text['nextPage']
    nextdate = text['nextdate']
    return (dates, nextdate, nextPage)


def count_process_Kxun():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 0
    nowdate = time.strftime("%Y-%m-%d %X", time.localtime())[:10]
    page = 1
    while 1:
        data = {
            'page': page,
            'newsclass': 'flash',
            'length': '15',
            'nowdate': nowdate,
            'keyword': ''
        }
        url = 'https://www.xcj.com/newsflash/index'
        dates = get_dates_Kx(url, data)
        if not dates:
            break
        nowdate = dates[1]
        page = dates[2]
        for date in dates[0]:
            s = day(date[:10])
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        print('----新财经快讯第{}页获取中----'.format(a))
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '新财经237'
    t1 = count_process_Kxun()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0], "yesterday": t1[1], "frontday": t1[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
